package com.dolphin.core.response;

import com.dolphin.core.exception.BaseException;
import com.dolphin.core.exception.BaseResponse;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * @author shida
 */
@Setter
@Getter
@ToString
public class BaseVO<T> implements Serializable {

    private String responseCode;
    private String responseMsg;

    private T data;
    private Long elapsedTime;

    private BaseVO(){}

    public static <T> BaseVO<T> succ(T data){
        BaseVO<T> vo = new BaseVO<>();

        vo.setData(data);
        vo.setResponseCode(BaseResponse.SUCC.getCode());
        vo.setResponseMsg(BaseResponse.SUCC.getMsg());

        return vo;
    }

    public static <T> BaseVO<T> fail(String code, String msg, T data){
        BaseVO<T> vo = new BaseVO<>();

        vo.setResponseCode(code);
        vo.setResponseMsg(msg);
        vo.setData(data);

        return vo;
    }

    public static <T> BaseVO<T> fail(){
        return fail(BaseResponse.FAIL.getCode(), BaseResponse.FAIL.getMsg(), null);
    }

    public static <T> BaseVO<T> fail(BaseResponse baseResponse){
        return fail(baseResponse.getCode(), baseResponse.getMsg(), null);
    }

    public static <T> BaseVO<T> fail(BaseException e){
        return fail(e.getResponseCode(), e.getResponseMsg(), (T)e.getData());
    }

    public static <T> BaseVO<T> fail(BaseResponse response, T data){
        return fail(response.getCode(), response.getMsg(), data);
    }

    public static <T> BaseVO<T> fail(T data){
        return fail(BaseResponse.FAIL.getCode(), BaseResponse.FAIL.getMsg(), data);
    }

}
